import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
  //data
  state: {
    userinfo: {},
    needAuth: true,     // 是否需要授权
    isLogin: false,     // 登陆状态
	keyword:'搜索',
    menu: [{
        title: "技术分享",
        url: '/subpackage/technology_share/technology_share',
        icon: "/static/nav_icon/technique_sharing@3x.png"
      },
      {
        title: "故障消缺",
        url: '/subpackage/technology_share/technology_fault',
        icon: "/static/nav_icon/shebeifuwu@2x.png"
      },
      {
        title: "小工具集",
        url: '/subpackage/tool_set/tool_set',
        icon: "/static/nav_icon/widget_set.png"
      },
      {
        title: "行业标准",
        url: '/subpackage/industry_standard/industry_standard',
        icon: "/static/nav_icon/industry_standard@3x.png"
      },
      {
        title: "资料下载",
        url: '/subpackage/file_download/file_download',
        icon: "/static/nav_icon/data_download@3x.png"
      },
      {
        title: "需求与服务",
        url: '/subpackage/wind/build/build',
        icon: "/static/nav_icon/offshore_wind.png"
      },
	  {
	    title: "风电厂家",
	    url: '/subpackage/wind/company/company',
	    icon: "/static/nav_icon/wind_power_manufacturers@3x.png"
	  },
	  {
	    title: "项目合作",
	    url: '/subpackage/wind/cooperate/cooperate_web',
	    icon: "/static/nav_icon/project_cooperation@3x.png"
	  },
      // {
      //   title: "二手市场",
      //   url: '/subpackage/wind/expect/expect',
      //   icon: "/static/nav_icon/secondarymarket@3x.png"
      // },
      // {
      //   title: "风电配件",
      //   url: '/subpackage/wind/expect/expect',
      //   icon: "/static/nav_icon/windpoweraccessories@3x.png"
      // },
      
    ],
  },
  //computed
  getters: {
    getUserinfo(state) {
      return state.userinfo
    },
    getNeedAuth(state) {
      return state.needAuth
    },
    getIsLogin(state) {
      return state.isLogin
    },
    getMenu(state){
      return state.menu
    }
  },
  //methods 同步
  mutations: {
    setUserinfo(state, userinfo) {
      state.userinfo = userinfo;
    },
    setNeedAuth(state, needAuth) {
      state.needAuth = needAuth;
    },
    setIsLogin(state, isLogin) {
      state.isLogin = isLogin;
    },
  },
  //异步的方法
  actions: {
    login(context) {
      var param = new Promise((resolve, reject) => {
        console.log(uni.getStorageSync('token'),"token")
        uni.removeStorageSync('token')
        uni.login({
          provider: 'weixin',
          success: res => {
            uni.setStorageSync('wx_code', res.code);
            Vue.prototype.$u.api.login({
              appid: Vue.prototype.appid,
              code: res.code,
              token: uni.getStorageSync('token')
            }).then(res => {
              uni.setStorageSync('token', res.data.token);
              uni.setStorageSync('sessionKey', res.data.sessionKey);
              uni.setStorageSync('openid', res.data.openid);
              if (res.msg == "登录成功") {
                context.commit('setUserinfo', res.data.user);
                context.commit('setNeedAuth', false);
                context.commit('setIsLogin', true);
              }
              resolve(res.data.user);
            })
          },
          fail: res => {
            Vue.prototype.$u.toast("获取code失败");
            reject("获取code失败");
          }
        });
      })
      return param
    },

    authUserInfo(context) {
      let that = this;
      return new Promise((resolve, reject) => {
        wx.getUserProfile({
          lang: 'zh_CN',
          desc: '注册',
          success: (res) => {
            console.log(res,'resresres')
            context.commit('setUserinfo', {
              nickname: res.userInfo.nickName,
              sex: res.userInfo.gender,
              avatar: res.userInfo.avatarUrl,
              city: res.userInfo.city,
              signature:res.signature,
              rawData: res.rawData,
              appid:Vue.prototype.appid,
              sessionKey:uni.getStorageSync('sessionKey'),
            })
            context.commit('setNeedAuth', false);

            // Vue.prototype.$u.api.auth(context.state.userinfo).then(res => {
            //   if (res.msg == "授权成功") {
            //     context.commit('setUserinfo', res.data);
                
            //     context.commit('setNeedAuth', false);
            //     context.commit('setIsLogin', true);
            //   } else {
            //     Vue.prototype.$u.toast("授权失败")
            //   }
            //   resolve(res.data);
            // })
          },
          fail: err => {
            reject("获取信息出错")
          }
        })
      })
    },
    authphoneNumber(context){
      let that = this;
      return new Promise((resolve, reject) => {
		  
		          // Vue.prototype.$u.api.auth(context.state.userinfo).then(res => {
		          //   if (res.msg == "授权成功") {
		          //     context.commit('setUserinfo', res.data);
		          //     context.commit('setNeedAuth', false);
		          //     context.commit('setIsLogin', true);
		          //   } else {
		          //     Vue.prototype.$u.toast("授权失败")
		          //   }
		          //   resolve(res.data);
		          // })
    //     uni.login({
    //     	provider: 'weixin',
    //     	success: function(loginRes) {
				
    //     		console.log(loginRes,'ssssssssssssssssssssssss');
    //         Vue.prototype.$u.api.auth(context.state.userinfo).then(res => {
    //           if (res.msg == "授权成功") {
    //             context.commit('setUserinfo', res.data);
    //             context.commit('setNeedAuth', false);
    //             context.commit('setIsLogin', true);
    //           } else {
    //             Vue.prototype.$u.toast("授权失败")
    //           }
    //           resolve(res.data);
    //         })
    //     	},
    //       fail: err => {
    //         reject("获取信息出错")
    //       }
    //     });
       
      })
    }
  }
})
export default store
